<template>
	<view class="mainpadding" v-if="pageShow">
		<!-- 选择性别 -->
		<u-picker :show="xbshow" :columns="xbcolumns" :confirmText="$t('system.confirm')" :cancelText="$t('system.cancel')" :title="$t('editinfo.xzxb')" @cancel="xbshow=false" @confirm="xbconfirm"></u-picker>
		<view class="flexcenter margin_top5" @click="uploadImg(1,avatar,avatar_text)">
			<view class="touxiang flexcenter">
				<image :src="avatar" mode=""></image>
				<view class="toubox flexcenter">
					<u-icon name="camera" color="#fff" size="38"></u-icon>
				</view>
			</view>
		</view>
		<view class="">
			<view class="flexbetween margin_top6" @click="jumper('/pages/mine/xiugainc?name=',nickname)">
				<view class="sanshier xiaohui">{{$t('editinfo.nc')}}</view>
				<view class="flexright">
					<view class="sanshier margin_right1">{{nickname}}</view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			<view class="flexbetween margin_top6">
				<view class="sanshier xiaohui">{{$t('editinfo.idh')}}</view>
				<view class="sanshier">{{userinfo.userId}}</view>
			</view>
			<view class="flexbetween margin_top6" @click="xbshow = true">
				<view class="sanshier xiaohui">{{$t('editinfo.xb')}}</view>
				<view class="flexright">
					<view class="sanshier margin_right1">{{gender}}</view>
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				xbshow: false,
				xbcolumns: [
					[this.$t('editinfo.nan'), this.$t('editinfo.nv')]
				],
				
				avatar: "",
				nickname: "",
				userinfo:{},
				gender: "",
				pageShow:true,
			}
		},
		onLoad() {
			this.init()
		},
		onShow() {
			uni.$off("editname")
			uni.$on("editname", data => {
				this.nickname = data
			})
		},
		methods: {
			init() {
				httpRequest.request('/api/v1/users/getUserInfo', 'GET', {}).then(res => {
					this.avatar = res.data.avatar
					this.gender = res.data.sex=="1" ? this.$t('editinfo.nv') : this.$t('editinfo.nan')
					this.nickname = res.data.nickName
					this.userinfo = res.data
					this.pageShow = true
				})
			},
			uploadImg() {
				let _this = this;
				uni.showActionSheet({
					itemList: [_this.$t('system.xj'), _this.$t('system.xc')],
					success: function(res) {
						if(res.tapIndex==0){
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}else{
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/v1/index/upload', url).then(res => {
					console.log(res);
					this.avatar = res.url;
					this.setinfo({
						avatar: res.url
					})
				}).catch(err => {});
			},
			// 性别
			xbconfirm(e) {
				this.xbshow = false
				if(e.value[0]==this.gender){
					return false
				}
				this.gender = e.value[0]
				this.setinfo({
					sex: this.gender == this.$t('editinfo.nan') ? 0 : 1
				})
			},
			setinfo(data) {
				httpRequest.request('/api/v1/users/editUser', 'POST', data).then(res => {
					if(res.code!=200){
						httpRequest.toast(res.msg)
					}
				})
			},
			jumper(url, text) {
				uni.navigateTo({
					url: url + text
				})
			},
		}
	}
</script>

<style lang="scss">
	/deep/.u-radio-group {
		flex: none !important;
	}
	.touxiang {
		width: 186rpx;
		height: 186rpx;
		border-radius: 50%;
		position: relative;
		z-index: 100;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}
	}

	.toubox {
		position: absolute;
		left: 0;
		top: 0;
		width: 186rpx;
		height: 186rpx;
		background: #000000;
		opacity: .3;
		opacity: 0.5;
		border-radius: 50%;
	}
</style>